import Session, { type SessionData } from "@/libs/session";
import { refreshToken } from '@/libs/cognitoClient';

class TokenCenter {
  private refreshing = false;
  private tasks: Function[] = [];

  // 刷新 token 请求
  private refreshTokenRequest = async (session: SessionData) => {
    this.refreshing = true;
    try {
      const result = await refreshToken(session);
      await Session.setSession(result);
      this.taskCompleted(result);
    } catch (err) {
      console.log(err)
      this.taskCompleted();
    }
    this.refreshing = false;
  }

  // 刷新 token 完成
  private taskCompleted = (session?: SessionData) => {
    this.tasks.forEach(fn => fn(session?.accessToken));
    this.tasks = [];
  }

  // 设置 token
  public setToken = (session: SessionData) => {
    return Session.setSession(session);
  }

  //获取 token
  public getToken = () => new Promise<string | undefined>(async (resovle) => {
    this.tasks.push(resovle);

    if (this.refreshing) {
      return;
    }

    const session = await Session.getSession();

    if (session?.expired) {
      this.refreshTokenRequest(session);
    } else {
      this.taskCompleted(session);
    }
  });
}

export default new TokenCenter();